package com.cs4261.smartalarm.backend;

import java.util.List;

import com.cs4261.smartalarm.weather.WeatherInfo;
import com.cs4261.smartalarm.weather.YahooWeatherUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.Toast;


public class WeatherHelper {
	private Context context;
	
	private LocationManager locationManager;
	double longitude;
	double latitude;
	String bestProvider;
	List<Address> address = null;
	
	public WeatherHelper(Context context) {
		this.context = context;
	}
	
	//Temp, fix for proper etiquette
	@SuppressLint("NewApi")
	public String getCurrentWeatherCondition() {		
		Geocoder geocoder;		
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		bestProvider = locationManager.getBestProvider(criteria, true);
		Location location = locationManager.getLastKnownLocation(bestProvider);
		
		
		
		 if (location == null){
	         //Toast.makeText(context,"Location Not found",Toast.LENGTH_LONG).show();
	      }else{
	        geocoder = new Geocoder(context);
	        try {
	            address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
	            latitude=(double)address.get(0).getLatitude();
	            longitude=(double)address.get(0).getLongitude();
	            //Toast.makeText(context, " DDD lat: " +latitude+",  longitude: "+longitude + "\n" + address.get(0).getAddressLine(0) + "\n" + address.get(0).getAddressLine(1) + "\n" + address.get(0).getAddressLine(2) + "\n" + address.get(0).getAddressLine(3) + "\n" + address.get(0).getAddressLine(4), Toast.LENGTH_LONG).show();
	            
	            //Temp fix, remove new pai override
	            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	            StrictMode.setThreadPolicy(policy); 
	            
	            YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
	            WeatherInfo weatherInfo = yahooWeatherUtils.queryYahooWeather(context, address.get(0).getLocality());
	            /**String results = weatherInfo.getTitle() + "\n"
						+ weatherInfo.getLocationCity() + ", "
						+ weatherInfo.getLocationCountry() + "\n"
						+ "current weather: " + weatherInfo.getCurrentText() + "\n"
						+ "forecast weather: " + weatherInfo.getForecast1Text() + "\n" 
						+ "forecast weather: " + weatherInfo.getForecast2Text();**/
	            //Toast.makeText(context, "Results: " + address.get(0).getLocality() + ", " + weatherInfo.getLocationCity() + ", " + "current weather: " + weatherInfo.getCurrentText(), Toast.LENGTH_LONG).show();
	            
	            return weatherInfo.getCurrentText();

	        }catch (Exception e) {
	                e.printStackTrace();
	                return null;
	        }
	    }
		return null;
		
	}
	
	
	
	
	
	
	
	
}